package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/8 20:10
 * https://leetcode-cn.com/problems/diagonal-traverse/
 */
public class Title415 {

    public static String addStrings(String num1, String num2) {
        int[] result = new int[Math.max(num1.length(), num2.length()) + 1];
        int count = 0;
        for (int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0; i--, j--) {
            int x, y;
            if (i < 0) {
                x = 0;
            }else{
                x = num1.charAt(i) - '0';
            }
            if (j < 0) {
                y = 0;
            }else{
                y = num2.charAt(j) - '0';
            }
            result[count++] = x + y;
        }
        for (int i = 1; i < result.length; i++) {
            result[i] += result[i - 1] / 10;
            result[i - 1] %= 10;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = result.length - 1; i >= 0; i--) {
            if (result[i] == 0 && sb.length() == 0) {
                continue;
            }
            sb.append(result[i]);
        }
        if (sb.length() == 0) {
            return "0";
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String num1 = "11";
        String num2 = "123";
        addStrings(num1, num2);
    }
}
